package com.xyz.controller;

import com.xyz.aop.ObjectParam;
import com.xyz.aop.Param;
import com.xyz.dto.PatrolPlanDto;
import com.xyz.dto.PlanInfoDto;
import com.xyz.dto.UpkeepCalendarDto;
import com.xyz.entity.vo.UpkeepDbTaskExportVo;
import com.xyz.service.PlanInfoService;
import com.xyz.util.ExcelUtils;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.LoginInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("plan")
public class PlanInfoController {

    private static final Logger logger = LoggerFactory.getLogger(PlanInfoController.class);

    @Autowired
    private PlanInfoService planInfoService;


    /*
    * 查询计划列表
    * */
    @PostMapping("/getPlanAll")
    public DataResult getPlanAll(
            LoginInfo loginInfo,
            @ObjectParam PlanInfoDto planInfoDto
    ) {
        try {
            return planInfoService.getPlanAll(loginInfo, planInfoDto);
        } catch (Exception e) {
            logger.error("getPlanAll", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据日期查询计划
     * */
    @PostMapping("/getPlanTemplateByDate")
    public DataResult getPlanTemplateByDate(
            LoginInfo loginInfo,
            @ObjectParam PlanInfoDto planInfoDto
    ) {
        try {
            return planInfoService.getPlanTemplateByDate(loginInfo, planInfoDto);
        } catch (Exception e) {
            logger.error("getPlanTemplateByDate", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据日期查询计划
     * */
    @PostMapping("/selectTemplateGroup")
    public DataResult selectTemplateGroup(
            LoginInfo loginInfo,
            @ObjectParam PlanInfoDto planInfoDto
    ) {
        try {
            return planInfoService.selectTemplateGroup(loginInfo, planInfoDto);
        } catch (Exception e) {
            logger.error("selectTemplateGroup", e);
            return DataResult.build9500();
        }
    }

    /*
     * 查询巡检日历计划
     * */
    @PostMapping("/getPatrolPlanAll")
    public DataResult getPatrolPlanAll(
            LoginInfo loginInfo,
            @ObjectParam PatrolPlanDto patrolPlanDto
    ) {
        try {
            return planInfoService.getPatrolPlanAll(loginInfo, patrolPlanDto);
        } catch (Exception e) {
            logger.error("getPatrolPlanAll", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据日期查询巡检计划
     * */
    @PostMapping("/getPatrolPlanTemplateByDate")
    public DataResult getPatrolPlanTemplateByDate(
            LoginInfo loginInfo,
            @ObjectParam PatrolPlanDto patrolPlanDto
    ) {
        try {
            return planInfoService.getPatrolPlanTemplateByDate(loginInfo, patrolPlanDto);
        } catch (Exception e) {
            logger.error("getPatrolPlanTemplateByDate", e);
            return DataResult.build9500();
        }
    }

    /*
     * 查询模板下拉框
     * */
    @PostMapping("/selectPatrolTemplateGroup")
    public DataResult selectPatrolTemplateGroup(
            LoginInfo loginInfo,
            @ObjectParam PatrolPlanDto patrolPlanDto
    ) {
        try {
            return planInfoService.selectPatrolTemplateGroup(loginInfo, patrolPlanDto);
        } catch (Exception e) {
            logger.error("selectPatrolTemplateGroup", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据设备ID查询基础工作项
     * */
    @PostMapping("/selectUpkeepDbTask")
    public DataResult selectUpkeepDbTask(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String emId,
            @Param(isRequired = false) String taskName
    ) {
        try {
            return planInfoService.selectUpkeepDbTask(loginInfo, emId, taskName);
        } catch (Exception e) {
            logger.error("selectUpkeepDbTask", e);
            return DataResult.build9500();
        }
    }

    /*
     * 查询基础数据工作项
     * */
    @PostMapping("/selectBasicUpkeepDbTask")
    public DataResult selectBasicUpkeepDbTask(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(isRequired = false,type = Param.ParamType.POSITIVE_INTEGER) String treeId,
            @Param(isRequired = false,type = Param.ParamType.POSITIVE_INTEGER) String emId,
            @Param(isRequired = false) String taskName
    ) {
        try {
            return planInfoService.selectBasicUpkeepDbTask(loginInfo, Integer.parseInt(pageNum), Integer.parseInt(pageSize), treeId, emId, taskName);
        } catch (Exception e) {
            logger.error("selectBasicUpkeepDbTask", e);
            return DataResult.build9500();
        }
    }

    @ApiOperation(value = "维修计划管理导入")
    @PostMapping("/importBasicUpkeepDbTask")
    public DataResult importBasicUpkeepDbTask(
            LoginInfo li,
            @RequestParam("file")  MultipartFile file
    ) {
        try {
            List<UpkeepDbTaskExportVo> read = ExcelUtils.read(file, UpkeepDbTaskExportVo.class);
            return planInfoService.importBasicUpkeepDbTask(li,read);
        } catch (Exception e) {
            logger.error("importBasicUpkeepDbTask", e);
            return DataResult.build9500();
        }
    }

    /*
     * 保存基础维保计划任务
     * */
    @PostMapping("/insertUpkeepDbTask")
    public DataResult insertUpkeepDbTask(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String emId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String treeId,
            @Param String taskName,
            @Param(isRequired = false) String taskDesc,
            @Param(isRequired = false) String remark,
            @Param(isRequired = false) String precautions
    ) {
        try {
            return planInfoService.insertUpkeepDbTask(li, Long.parseLong(treeId), Long.parseLong(emId), taskName, taskDesc, remark,precautions);
        } catch (Exception e) {
            logger.error("insertUpkeepDbTask", e);
            return DataResult.build9500();
        }
    }

    /*
     * 编辑基础维保计划任务
     * */
    @PostMapping("/updateUpkeepDbTask")
    public DataResult updateUpkeepDbTask(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String emId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String treeId,
            @Param String taskName,
            @Param(isRequired = false) String taskDesc,
            @Param(isRequired = false) String remark,
            @Param(isRequired = false) String precautions
    ) {
        try {
            return planInfoService.updateUpkeepDbTask(li, Long.parseLong(id), Long.parseLong(treeId), Long.parseLong(emId), taskName, taskDesc, remark, precautions);
        } catch (Exception e) {
            logger.error("updateUpkeepDbTask", e);
            return DataResult.build9500();
        }
    }

    /*
     * 删除基础维保计划任务
     * */
    @PostMapping("/deleteUpkeepDbTask")
    public DataResult deleteUpkeepDbTask(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return planInfoService.deleteUpkeepDbTask(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("deleteUpkeepDbTask", e);
            return DataResult.build9500();
        }
    }

    /*
     * 查询维保计划日历列表
     * */
    @PostMapping("/getUpkeepCalendar")
    public DataResult getUpkeepCalendar(
            LoginInfo loginInfo,
            @ObjectParam UpkeepCalendarDto upkeepCalendarDto
    ) {
        try {
            return planInfoService.getUpkeepCalendar(loginInfo, upkeepCalendarDto);
        } catch (Exception e) {
            logger.error("getUpkeepCalendar", e);
            return DataResult.build9500();
        }
    }

    /*
     * 查询维保计划日历列表设备组下拉框
     * */
    @PostMapping("/selectUpkeepCalendarEm")
    public DataResult selectUpkeepCalendarEm(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String treeId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String year
    ) {
        try {
            return planInfoService.selectUpkeepCalendarEm(loginInfo, Long.parseLong(treeId), Integer.parseInt(year));
        } catch (Exception e) {
            logger.error("selectUpkeepCalendarEm", e);
            return DataResult.build9500();
        }
    }

}
